<?php

class Jf_Tree extends Jf_Tree_Container {

    public function __construct ($adapter, $nodes = null) {
        if (is_string ($adapter) &&  ! empty ($adapter)) {
            $this -> setAdapter ($adapter);
        } else {
            throw new Jf_Tree_Exception ('Invalid argument: $adapter must be an string');
        }
        if (is_array ($nodes)) {
            $this -> addNodes ($nodes);
        }
    }

    public static function UrlFactory ($options) {
        if ($options instanceof Zend_Config) {
            $options = $options -> toArray ();
        }
        if ( ! is_array ($options)) {
            throw new Jf_Tree_Exception ('Invalid argument: $options must be an array or Zend_Config');
        }
        $type = null;
        if (isset ($options['type'])) {
            $type = $options['type'];
        }
        if (isset ($type)) {
            if (is_string ($type) &&  ! empty ($type)) {
                switch (strtolower ($type)) {
                    case 'mvc' :
                        $type = 'Jf_Tree_Href_Mvc';
                        break;
                    case 'uri' :
                        $type = 'Jf_Tree_Href_Uri';
                        break;
                }
                if ( ! class_exists ($type)) {
                    @Zend_Loader::loadClass ($type);
                }
                $node = new $type ($options);
                if ( ! $node instanceof Jf_Tree_Node) {
                    throw new Jf_Tree_Exception (sprintf ('Invalid argument: Detected type "%s", which ' . 'is not an instance of Jf_Tree_Node', $type));
                }
                return $node;
            }
        }
        $hasUri = isset ($options['uri']);
        $hasMvc = isset ($options['action']) || isset ($options['controller']) || isset ($options['module']) || isset ($options['route']);
        if ($hasMvc) {
            return new Jf_Tree_Href_Mvc ($options);
        } elseif ($hasUri) {
            return new Jf_Tree_Href_Uri ($options);
        } else {
            $adapter = "Jf_Tree_Adapter_" . self::$_adapter;
            return new $adapter ($options);
            /*
             * $message = 'Invalid argument: Unable to determine class to instantiate'; if (isset($options['label'])) { $message .= ' (Node label: ' . $options['label'] . ')'; } throw new Jf_Tree_Exception($message);
             */
        }
    }

}
